import { ResPage, ResultData } from "@/api/interface/index";
import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";

export namespace Process {
  export interface ProcessListItem {
    id: string; // 字符串类型以避免大数精度丢失
    processCode: string;
    processName: string;
    workOrderId?: string;
    routeStepId?: string;
    workstationId?: string;
    equipmentId?: string;
    status: string;
    createdTime?: string | number[] | Date;
    updatedTime?: string | number[] | Date;
    createdBy?: string;
    updatedBy?: string;
  }

  export interface CreateProcessRequest {
    processCode: string;
    processName?: string;
    parameters?: Record<string, string>;
  }

  export interface StartProcessRequest {
    processId: string;
    workOrderId: string;
    equipmentId: string;
    operatorId: string;
    plannedStartTime?: string;
    remarks?: string;
  }

  export interface PauseProcessRequest {
    processId: string;
    operatorId: string;
    reason: string;
  }

  export interface ResumeProcessRequest {
    processId: string;
    operatorId: string;
  }

  export interface CompleteProcessRequest {
    processId: string;
    operatorId: string;
  }

  export interface AssignProcessRequest {
    processId: string;
    workstationId: string;
    equipmentId: string;
  }

  export interface HoldProcessRequest {
    processId: string;
    operatorId: string;
    reason: string;
  }

  export interface ReleaseProcessRequest {
    processId: string;
    operatorId: string;
  }

  export interface ReworkProcessRequest {
    processId: string;
    operatorId: string;
    reason: string;
  }

  export interface ChangeProcessStepRequest {
    processId: string;
    newStepId: string;
    operatorId: string;
    reason: string;
  }

  export interface CheckProcessQualityRequest {
    processId: string;
    operatorId: string;
    qualityResult: string;
    remarks?: string;
  }

  export interface RecordProcessMeasurementRequest {
    processId: string;
    operatorId: string;
    measurements: Record<string, any>;
  }

  export interface ProcessListParams {
    current?: number;
    size?: number;
    processCode?: string;
    processName?: string;
    status?: string;
    workOrderId?: string;
  }
}

/**
 * @name 工序管理模块
 */
// 获取工序列表
export const getProcessList = (params: Process.ProcessListParams) => {
  return http.get<ResPage<Process.ProcessListItem>>(`/process/list`, params);
};

// 创建工序
export const createProcess = (params: Process.CreateProcessRequest) => {
  return http.post<ResultData<number>>(`/process/create`, params);
};

// 获取工序详情
export const getProcessById = (id: string) => {
  return http.get<ResultData<Process.ProcessListItem>>(`/process/${id}`);
};

// 开始工序
export const startProcess = (params: Process.StartProcessRequest) => {
  return http.post<ResultData<void>>(`/process/start`, params);
};

// 暂停工序
export const pauseProcess = (params: Process.PauseProcessRequest) => {
  return http.post<ResultData<void>>(`/process/pause`, params);
};

// 恢复工序
export const resumeProcess = (params: Process.ResumeProcessRequest) => {
  return http.post<ResultData<void>>(`/process/resume`, params);
};

// 完成工序
export const completeProcess = (params: Process.CompleteProcessRequest) => {
  return http.post<ResultData<void>>(`/process/complete`, params);
};

// 分配工序
export const assignProcess = (params: Process.AssignProcessRequest) => {
  return http.post<ResultData<void>>(`/process/assign`, params);
};

// 挂起工序
export const holdProcess = (params: Process.HoldProcessRequest) => {
  return http.post<ResultData<void>>(`/process/hold`, params);
};

// 解除挂起
export const releaseProcess = (params: Process.ReleaseProcessRequest) => {
  return http.post<ResultData<void>>(`/process/release`, params);
};

// 工序返工
export const reworkProcess = (params: Process.ReworkProcessRequest) => {
  return http.post<ResultData<void>>(`/process/rework`, params);
};

// 变更工序步骤
export const changeProcessStep = (params: Process.ChangeProcessStepRequest) => {
  return http.post<ResultData<void>>(`/process/changeStep`, params);
};

// 工序质量检测
export const checkProcessQuality = (params: Process.CheckProcessQualityRequest) => {
  return http.post<ResultData<void>>(`/process/checkQuality`, params);
};

// 工序测量参数记录
export const recordProcessMeasurement = (params: Process.RecordProcessMeasurementRequest) => {
  return http.post<ResultData<void>>(`/process/recordMeasurement`, params);
};

// 删除工序
export const deleteProcess = (id: string) => {
  return http.delete<ResultData<void>>(`/process/${id}`);
};

// 批量删除工序
export const batchDeleteProcess = (ids: string[]) => {
  return http.delete<ResultData<void>>(`/process/batch`, { data: { ids } });
};
